var service_user = require("../../service/user.js");
var service_adv = require("../../service/adv.js");
var service_common = require("../../service/common.js");
var service_teacher = require("../../service/teacher.js");
var service_view = require("../../service/view.js");
var isAdmination = 0;
var app = getApp();
Page({
	data: {
		admination: {},
		selectedTemplate: '',
		userInfo: {},
		photo_css: "display:none;",
		slide_tip: "display:none;",
		animation: "",
		options: {},
		adv_css: "display:none;",
		show_more: "display:none;",
		signModal: "display:none;",
		wrq_news: 0,
		user_news: 0,
		wrq_zan: 0,
		tc_userInfo: false
	},
	getHongBao: function() {
		wx.navigateToMiniProgram({
			appId: 'wx36e289fcc22707ec',
			path: 'pages/send/send?openid=' + getApp().globalData.userInfo.openid + "&id=" + getApp().globalData.userInfo.hongBao +
				"&from=yzmp",
		})
	},
	// 前往雷达
	goLd: function() {
		wx.navigateTo({
			url: '/pages/ld_list/ld_list',
		})
	},
	putHongBao: function() {
		wx.navigateToMiniProgram({
			appId: 'wx36e289fcc22707ec',
			path: "pages/index/index?openid=" + getApp().globalData.userInfo.openid + "&from=yzmp",
		})
	},
	onLoad: function(options) {
		console.log('--------------------------------------------')
		console.log(getApp().globalData.openid_id)
		console.log('===========================================-')
		var that = this
		wx.setStorageSync("isred", true)
		this.setData({
			options: options
		})
		wx.showLoading({
			title: '登录中',
			mask: true
		})
	},

	/**
	 * 关闭签到弹窗
	 */
	closeSignModal: function() {

		wx.setStorage({
			key: 'last_sign_time',
			data: new Date().getTime() / 1000,
		})
		this.setData({
			signModal: "display:none;"
		})
	},
	//
	cardFormId: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
	},
	onShow: function() {
		var that = this;
		wx.setNavigationBarTitle({
			title: "我的名片"
		})
		if (this.data.options.scene != undefined) {
			var scene = decodeURIComponent(this.data.options.scene);
			var cid = scene.split("=")[0];
			var tid = scene.split("=")[1];
			this.data.options.class_id = cid;

			service_user.getOpenIdById(tid, function(res) {
				service_user.login(that.reloadUserInfoBack, res.data.openid);
			})
		} else {
			if (this.data.options.class_id != undefined && this.data.options.class_id != "" && this.data.options.class_id !=
				0) {
				service_user.login(this.reloadUserInfoBack, this.data.options.jiang_shi_openid);
			} else {
				service_user.login(this.reloadUserInfoBack);
			}
		}
		if (app.globalData.currentStatu == "open") {
			that.sendCard();
			app.globalData.currentStatu = "close"
		}
		//service_user.getUserNews(cb);
	},
	/**
	 * 获取万人圈新消息数
	 */
	newReplyCount: function(res) {
		console.log(res)
		// 
		this.setData({
			wrq_news: res.data.data.new_reply_count
		})
	},
	// 足迹
	tozuJi: function() {
		wx.navigateTo({
			url: '/pages/zuJI/zuJI',
		})
	},
	// 获取万人圈点赞个数
	wrq_zan: function() {
		service_common.fetchData(
			service_common.getUrl("user.newLoveCount"), {
				openid: getApp().globalData.userInfo.openid
			},
			(res) => {
				// 
				this.setData({
					wrq_zan: res.data.data.new_love_count
				})
			},
			"GET"
		)
	},
	toMyPublish: function() {
		wx.navigateTo({
			url: '/pages/demand_my/demand_my',
		})
	},
	//图片预览
	showPhoto: function(e) {
		console.log(1)
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		var id = e.currentTarget.id;
		id = parseInt(id);
		var array = [];
		for (var i = 0; i < this.data.image_array.length; i++) {
			array[i] = this.data.image_array[i].b;
		}
		wx.previewImage({
			current: array[id],
			urls: array
		})
	},
	toAdv: function() {
		wx.navigateTo({
			url: '/pages/adv_list/adv_list',
		})
	},
	// 预览图片交流群
	toJiaoLiu: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		service_user.getQun()
	},

	// 更多按钮
	showMore: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		if (this.data.show_more == "") {
			this.setData({
				show_more: "display:none;"
			})
		} else {
			this.setData({
				show_more: ""
			})
		}
	},

	/**
	 * 跳转模板中心
	 */
	toTemp: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		wx.navigateTo({
			url: '../template/template',
		})
	},

	// 授权
	bindGetUserInfo: function(e) {
		if (e.detail.userInfo) {
			this.setData({
				tc_userInfo: false
			})
			this.onShow()
		}
	},

	// 重加载用户资料
	reloadUserInfoBack: function(userInfo) {
		if (userInfo == false) { //未授权
			wx.hideLoading()
			this.setData({
				tc_userInfo: true
			})
		} else {
			if (userInfo.ban == 1) {
				wx.redirectTo({
					url: '../ban/ban',
				})
				return;
			}
			var that = this;
			that.setData({
				userInfo: userInfo,
			})
			service_user.getToken(userInfo.openid)
			if (this.data.options.class_id != undefined && this.data.options.class_id != "" && this.data.options.class_id !=
				"0") {
				service_teacher.listenClass(this.data.options.class_id, function(res) {
					if (res.data.code == 0) {
						wx.showModal({
							title: '提示',
							content: "听课成功",
							showCancel: false,
							success: res2 => {
								that.data.options.class_id = 0;
								that.data.options.scene = null;
								wx.navigateTo({
									url: '../other/other?openid=' + res.data.data,
								})
							}
						})
					} else if (res.data.code == 10027) {
						wx.showModal({
							title: '提示',
							content: res.data.message,
							showCancel: false,
							success: res2 => {
								that.data.options.class_id = 0;
								that.data.options.scene = null;
							}
						})
					} else if (res.data.code == 10028) {
						wx.showModal({
							title: '提示',
							content: res.data.message,
							showCancel: false,
							success: res2 => {
								that.data.options.class_id = 0;
								that.data.options.scene = null;
								wx.navigateTo({
									url: '../other/other?openid=' + res.data.data,
								})
							}
						})
					} else if (res.data.code == 10029) {
						wx.showModal({
							title: '提示',
							content: res.data.message,
							showCancel: false,
							success: res2 => {
								that.data.options.class_id = 0;
								that.data.options.scene = null;
								wx.navigateTo({
									url: '../other/other?openid=' + res.data.data,
								})
							}
						})
					} else if (res.data.code == 10030) {
						wx.showModal({
							title: '提示',
							content: res.data.message,
							showCancel: false,
							success: res2 => {
								that.data.options.class_id = 0;
								that.data.options.scene = null;
							}
						})
					} else {
						wx.showModal({
							title: '提示',
							content: res.data.message,
							showCancel: false,
							success: res => {
								that.data.options.class_id = 0;
								that.data.options.scene = null;
							}
						})
					}
				});
			}
			var last_sign_time = wx.getStorageSync("last_sign_time");
			console.log("今天凌晨", new Date(new Date().setHours(0, 0, 0, 0)) / 1000)
			console.log("上次签到", last_sign_time)
			if (last_sign_time < new Date(new Date().setHours(0, 0, 0, 0)) / 1000 && getApp().globalData.isSign == 0) {
				that.setData({
					signModal: ""
				})
			} else {
				that.setData({
					signModal: "display:none;"
				})
			}

			service_user.getUserImage(getApp().globalData.userInfo.openid, function(res) {
				that.setData({
					image_array: res.data.data,
				})
				if (res.data.data.length > 0) {
					that.setData({
						photo_css: ""
					})
				}
				if (res.data.data.length > 3) {
					that.setData({
						slide_tip: ""
					})
				} else {
					that.setData({
						slide_tip: "display:none;"
					})
				}
			});
			wx.hideLoading()

			wx.stopPullDownRefresh();
		}
	},

	//跳转广告列表
	toAdvList: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		wx.navigateTo({
			url: "../adv_list/adv_list"
		})
	},
	toEdit: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		if (e.currentTarget.dataset.offon == '1') {
			wx.navigateTo({
				url: '/pages/edit/edit'
			})
		}

	},

	/**
	 * 展示隐藏电话
	 */
	showMobile: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		var that = this;
		var userInfo = this.data.userInfo;

		if (userInfo.mobile_visible == 0) {
			service_user.changeMobileVisible(1, function() {
				that.onShow()
			});
		} else {
			service_user.changeMobileVisible(0, function() {
				that.onShow()
			});
		}
		this.setData({
			userInfo: userInfo
		})
	},
	toSign: function() {
		wx.setStorage({
			key: 'last_sign_time',
			data: new Date().getTime() / 1000,
		})
		wx.navigateTo({
			url: '../sign/sign',
		})
	},
	//转发设置
	onShareAppMessage: function() {

		// 设置转发的路径
		console.log(app.globalData.userInfo.openid)
		var share_path = 'pages/other/other?openid=' + app.globalData.userInfo.openid + "&r=" + Math.random(100000);
		return {
			title: "我是" + getApp().globalData.userInfo.nickname + getApp().globalData.share_title,
			desc: '一个名片社交的资源配对平台，找资源找合作你就来',
			path: share_path,
			success: function() {
				service_user.setUserShareTime(function(res) {
					if (res.data.code == 0) {
						wx.showToast({
							title: '递名片成功',
							icon: 'success',
							duration: 1000,
						});
						setTimeout(function() {
							wx.showToast({
								title: '获得一天展示权限！',
								icon: 'success',
								duration: 1000,
							});
						}, 1000)
					} else {
						wx.showToast({
							title: '递名片成功',
							icon: 'success',
							duration: 1500
						});
					}
				})
			},
			fail: res => {
				wx.showToast({
					title: '递名片失败',
					image: '../../image/cancel.png',
					duration: 2000
				});
			}
		}
	},
	//分享事件
	share: function(e) {
		if (!wx.canIUse('button.open-type.share')) {
			wx.showModal({
				title: '提示',
				content: '当前微信版本过低，无法使用该功能，请点击右上角转发。',
				showCancel: false
			})
		}
		add_form_id(e.detail.formId)
	},
	/**
	 * 人气列表
	 */
	toView: function(e) {
		add_form_id(e.detail.formId);
		if (e.currentTarget.dataset.offon == '1') {
			wx.navigateTo({
				url: '/pages/see_view/see_view'
			})
		}

	},

	/**
	 * 点赞列表
	 */
	toLove: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		if (e.currentTarget.dataset.offon == '1') {
			wx.navigateTo({
				url: '/pages/see_love/see_love'
			})
		}

	},

	/**
	 * 收藏列表
	 */
	toCollect: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId)
		if (e.currentTarget.dataset.offon == '1') {
			wx.navigateTo({
				url: '/pages/address/address'
			})
		}
	},

	//下拉刷新
	onPullDownRefresh: function() {
		this.onShow();
	},

	//跳转小程序码
	toCode: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		if (e.currentTarget.dataset.offon == '1') {
			service_user.getUserCode();
		}

		// wx.navigateTo({
		//   url: '../canvas/canvas',
		// })
	},
	// 前往官网
	toGw: function(e) {
		add_form_id(e.detail.formId);
		if (e.currentTarget.dataset.offon == '1') {
			wx.navigateTo({
				url: '/pages/gwang/index/index',
			})
		}

	},
	//代理升级
	gotokaidaili: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: "/pages/kaidaili/kaidaili"
		})
	},
	//分享动画
	sendCard: function() {
		var currentStatu = app.globalData.currentStatu;
		/* 动画部分 */
		// 第1步：创建动画实例   
		var animation = wx.createAnimation({
			duration: 200, //动画时长  
			timingFunction: "linear", //线性  
			delay: 0 //0则不延迟  
		});
		// 第2步：这个动画实例赋给当前的动画实例  
		this.animation = animation;
		// 第3步：执行第一组动画  
		animation.opacity(0).rotateX(-100).step();
		// 第4步：导出动画对象赋给数据对象储存  
		this.setData({
			animationData: animation.export()
		})
		// 第5步：设置定时器到指定时候后，执行第二组动画  
		setTimeout(function() {
			// 执行第二组动画  
			animation.opacity(1).rotateX(0).step();
			// 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
			this.setData({
				animationData: animation
			})
			//关闭  
			if (currentStatu == "close") {
				this.setData({
					showModalStatus: false
				});
			}
		}.bind(this), 200)

		// 显示  
		if (currentStatu == "open") {
			this.setData({
				showModalStatus: true
			});
		}
		setTimeout(function() {
			this.setData({
				showModalStatus: false
			});
		}.bind(this), 1600)
	},

	// 点击空白生成formid
	no_action: function(e) {
		add_form_id(e.detail.formId);
	},

	/**
	 * 上传头像
	 */
	updateHeadImg: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		var that = this;
		if (e.currentTarget.dataset.offon == '2') {
			return
		}
		wx.chooseImage({
			count: 1,
			success: function(res) {
				wx.showLoading({
					title: '保存中',
					mask: true
				})
				var filePath = res.tempFilePaths[0];
				wx.uploadFile({
					url: getApp().globalData.API_Base_URL + "/uri/user/uploadHeadImage",
					filePath: filePath,
					name: "file",
					header: {
						"Content-Type": "multipart/form-data"
					},
					formData: {
						'version': getApp().globalData.version,
						'openid': getApp().globalData.userInfo.openid,
						'user_id': getApp().globalData.userInfo["id"],
					},
					success: function(res) {
						wx.hideLoading();
						if (res.statusCode == 200) {
							getApp().globalData.userInfo.src = filePath;
							that.setData({
								userInfo: getApp().globalData.userInfo
							})
						} else {
							wx.showModal({
								title: '提示',
								content: '上传失败，请重试',
								showCancel: false,
								success: res => {
									//that.getUserImage()
								}
							})
						}
					}
				})
			}
		})
	},

	/**
	 * 关注公众号
	 */
	follow: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		setTimeout(function() {
			service_common.fetchData(
				service_common.getUrl("user.sendKeFuMessage"), {
					openid: getApp().globalData.openid,
				},
				function(res) {
					cb(res);
				},
				"GET"
			)
		}, 1500)
	}

})
//添加form_id
function add_form_id(form_id) {
	service_user.addFormId(form_id);
}
